package com.ym521.emojipack.keyboard.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ym521.emojipack.R;
import com.ym521.emojipack.emoji.EmojiPackBean;
import com.ym521.emojipack.keyboard.EmoiiPackClickListener;
import com.ym521.emojipack.keyboard.adapter.viewHolder.EmojiPackGroupViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ym
 * E-mail: 2435970206@qq.com
 * createTime:2023/9/24
 * explain:
 */
public final class EmojiPackGroupAdapter extends RecyclerView.Adapter<EmojiPackGroupViewHolder> {
    private List<EmojiPackBean> dataList = new ArrayList<>();
    private boolean useSystemDefault;
    private EmoiiPackClickListener listener;

    public void addAllData(List<EmojiPackBean> dataList,boolean useSystemDefault) {
        int index = this.dataList.size();
        this.dataList.addAll(dataList);
        notifyItemRangeInserted(index, dataList.size());
    }

    public void flushData(EmojiPackBean data, boolean useSystemDefault) {
        this.useSystemDefault = useSystemDefault;
        int old = this.dataList.size();
        this.dataList.clear();
        this.dataList.add(data);
        notifyItemRangeRemoved(0, old);
        notifyItemInserted(0);
    }

    @NonNull
    @Override
    public EmojiPackGroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.emojipack_group_item, parent, false);
        return new EmojiPackGroupViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull EmojiPackGroupViewHolder holder, int position) {
        EmojiPackBean itemData = this.dataList.get(position);
        holder.showView(itemData, useSystemDefault, listener);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void setListener(EmoiiPackClickListener listener) {
        this.listener = listener;
    }
}
